package com.atguigu.yygh.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.yygh.feign.DictFeignClient;
import com.atguigu.yygh.model.hosp.BookingRule;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.repository.HospitalRepository;
import com.atguigu.yygh.service.IHospitalService;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author kabuda
 * @create 2023-02-25 19:59
 */
@Service
public class HospitalServiceImpl implements IHospitalService {

    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictFeignClient dictFeignClient;


    @Override
    public void saveHospital(Map<String, Object> map) {
        //将map转为String
        String string = JSON.toJSONString(map);
        //将String转为Hospital
        Hospital hospital = JSON.parseObject(string, Hospital.class);

        //图片传输问题，"+"转为" "
        //String logoData = hospital.getLogoData();
        //String replace = logoData.replace(" ", "+");
        //hospital.setLogoData(replace);

        //判断mongodb中是否存储该数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalMongo = hospitalRepository.getHospitalByHoscode(hoscode);

        if (hospitalMongo != null) {
            //存在,修改
            hospital.setCreateTime(hospitalMongo.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setStatus(hospitalMongo.getStatus());
            hospital.setIsDeleted(0);
        } else {
            //不存在，添加
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setStatus(0);
            hospital.setIsDeleted(0);
        }
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> list(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable= PageRequest.of(page-1,limit);
        ExampleMatcher exampleMatcher=ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase();
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example example=Example.of(hospital,exampleMatcher);
        Page all = hospitalRepository.findAll(example, pageable);
        List<Hospital> content = all.getContent();
        content.forEach(item ->{
            setHospitalHostype(item);
        });

        return all;
    }

    private Hospital setHospitalHostype(Hospital item){
        String hostypeString = dictFeignClient.getName("Hostype", item.getHostype());
        String city = dictFeignClient.getName( item.getCityCode());
        String district = dictFeignClient.getName( item.getDistrictCode());
        String province = dictFeignClient.getName( item.getProvinceCode());
        item.getParam().put("hostypeString",hostypeString);
        item.getParam().put("fullAdress",province+city+district);
        return  item;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Optional<Hospital> hospitalOptional = hospitalRepository.findById(id);
        Hospital hospital = hospitalOptional.get();

        hospital.setStatus(status);
        hospital.setCreateTime(new Date());
        hospitalRepository.save(hospital);

    }

    @Override
    public Map<String, Object> showHospitalInfo(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital=setHospitalHostype(hospital);

        BookingRule bookingRule = hospital.getBookingRule();
        Map<String, Object> map = new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",bookingRule);
        //不需要重复返回，map传递的是地址引用，所以设置为null，在put前后都行
        //map的put的值，其实就是将value值的引用指向放入value的对象，属于对象的引用，指针的变化，所以当这个对象在后期变化时，value值也会随之变化
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public List<Hospital> findByHosName(String hosname) {
        List<Hospital> list=hospitalRepository.findByHosname(hosname);
        return list;
    }


}
